<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="编号" prop="billNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.billNo"-->
<!--          placeholder="请输入编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="托盘号" prop="trayNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.trayNo"-->
<!--          placeholder="请输入托盘号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="物料编号" prop="productNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.productNo"-->
<!--          placeholder="请输入物料编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="物料名称" prop="productName">-->
<!--        <el-input-->
<!--          v-model="queryParams.productName"-->
<!--          placeholder="请输入物料名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="物料单位" prop="productUnit">-->
<!--        <el-input-->
<!--          v-model="queryParams.productUnit"-->
<!--          placeholder="请输入物料单位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="数量" prop="qty">-->
<!--        <el-input-->
<!--          v-model="queryParams.qty"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="批次号" prop="batchNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.batchNo"-->
<!--          placeholder="请输入批次号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="操作" prop="operation">-->
<!--        <el-input-->
<!--          v-model="queryParams.operation"-->
<!--          placeholder="请输入操作"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['bill:billDetail:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['bill:billDetail:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['bill:billDetail:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['bill:billDetail:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->
    <!-- <el-row>
      <el-col :span="1.5">
        <GeneratePDF 
          :header-info="pdfConfig.headerInfo"
          :table-data="billDetailList"
          :records="true"
          :isBatch="true"
        />
      </el-col>
    </el-row> -->

    <el-table v-loading="loading" :data="billDetailList">
      <el-table-column label="序号" type="index" width="50" />
<!--      <af-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="托盘号" align="center" prop="trayNo" />
<!--      <el-table-column label="单据类型" align="center" prop="billType" />-->
      <el-table-column label="编号" align="center" prop="billNo" width="200"/>
      <el-table-column label="库存状态" align="center" prop="stockStatus" />
      <el-table-column label="物料编号" align="center" prop="productNo" width="200"/>
      <el-table-column label="物料名称" align="center" prop="productName" width="300"/>
      <el-table-column label="物料单位" align="center" prop="cgUnitName" width="150"/>
      <el-table-column label="数量" align="center" prop="resultQty" />
      <el-table-column label="实发数量" align="center" prop="resultQty" />
      <el-table-column label="批次号" align="center" prop="batchNo" width="200"/>
<!--      <el-table-column label="操作" align="center" prop="operation" />-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="创建人" align="center" prop="createBy" min-width="100"/>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="152" />
      <el-table-column label="最后更新人" align="center" prop="updateBy" min-width="100"/>
      <el-table-column label="最后更新时间" align="center" prop="updateTime" min-width="152" />
    </el-table>

<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    />-->

    <!-- 添加或修改订单详细纪录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="billNo">
          <el-input v-model="form.billNo" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="托盘号" prop="trayNo">
          <el-input v-model="form.trayNo" placeholder="请输入托盘号" />
        </el-form-item>
        <el-form-item label="物料编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入物料编号" />
        </el-form-item>
        <el-form-item label="物料名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料单位" prop="productUnit">
          <el-input v-model="form.productUnit" placeholder="请输入物料单位" />
        </el-form-item>
        <el-form-item label="数量" prop="qty">
          <el-input v-model="form.qty" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="批次号" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入批次号" />
        </el-form-item>
        <el-form-item label="操作" prop="operation">
          <el-input v-model="form.operation" placeholder="请输入操作" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="是否删除：1是 0否" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除：1是 0否" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBillDetail, getBillDetail, delBillDetail, addBillDetail, updateBillDetail } from "@/api/invoice/billDetail";
import { listReceiveDetail } from '@/api/invoice/receive'
import GeneratePDF from '@/components/GeneratePDF/index';

import {getNowDate} from '@/utils/index.js'
import { selectAdjustOutBillForPrint } from "@/api/invoice/adjustOut";

export default {
  name: "BillDetail",
  components: {
    GeneratePDF
  },
  props: {
    orderData: {},
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selections: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单详细纪录表格数据
      billDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billNo: null,
        billType: null,
        trayNo: null,
        stockStatus: null,
        productNo: null,
        productName: null,
        productUnit: null,
        qty: null,
        batchNo: null,
        operation: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billNo: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        trayNo: [
          { required: true, message: "托盘号不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "是否删除：1是 0否不能为空", trigger: "blur" }
        ]
      },
      pdfConfig: {
        headerInfo: {
          deliveryNo: '2025042100000468',
          businessNo: 'DODL20250421-10',
          sender: '汤振伟',
          senderPhone: '15051848332',
          receiver: '当涂县南商业街店',
          receiverPhone: '赵敏 | 15221006043',
          deliveryAddress: '当涂县兴城路与富民大道交叉口西北40米',
          printTime: '2025-04-21 17:06:04',
          printer: '-1 | 管理员'
        }
      },
    };
  },
  watch: {
    // 监视订单信息变动
    orderData(newValue) {
      if (!newValue || !newValue.bizNo) {
        this.queryParams.bizNo = ''
      } else {
        this.queryParams.bizNo = newValue.bizNo
      }
      this.getList()
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单详细纪录列表 */
    getList() {
      this.loading = true;
      if (!this.queryParams.bizNo) {
        this.detailList = []
        this.total = 0
        this.loading = false
        return
      }
      listBillDetail(this.queryParams).then(response => {
        // this.billDetailList = response.rows;
        this.billDetailList = response.rows.map( el=>{
          const cgPsConver = Number(el.cgPsConver);
          const ylPsConver = Number(el.ylPsConver);
          // 防止除以零的情况
          const resultQty =cgPsConver && ylPsConver ? el.qty / (cgPsConver * ylPsConver) : '';
          return{
            ...el,
            resultQty: resultQty
          }
        })
        this.total = response.total;
        this.loading = false;
        // this.getMainInfo()
        this.$forceUpdate();
      });

    },
    getMainInfo(){
        selectAdjustOutBillForPrint({ no: this.orderData.bizNo }).then(response => {
          this.pdfConfig.headerInfo = response.data || {}
          this.pdfConfig.headerInfo.printTime = getNowDate()
           this.pdfConfig.headerInfo.billTitle = '出库调整单'
        })
      },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billNo: null,
        billType: null,
        trayNo: null,
        stockStatus: "0",
        productNo: null,
        productName: null,
        productUnit: null,
        qty: null,
        batchNo: null,
        operation: null,
        remark: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.selections = selection
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单详细纪录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBillDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单详细纪录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBillDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBillDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.resetQuery();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除"' + ids.length + '"条数据项？').then(function() {
        return delBillDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bill/billDetail/export', {
        ...this.queryParams
      }, `billDetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
